# -*- coding: utf-8 -*-
import argparse
import logging
import os
import sys
from logging.handlers import RotatingFileHandler

from src.handler.BrowserOpenHandler import BrowserOpenHandler
from src.handler.CloseComputerHandler import CloseComputerHandler
from src.handler.HandlerConfig import HandlerConfig
from src.handler.ProcessHandler import ProcessHandler
from src.handler.PushAutoOfficeHandler import PushAutoOfficeHandler
from src.handler.PushDingTalkHandler import PushDingTalkHandler
from src.handler.SoftStoneTaskHandler import SoftStoneTaskHandler
from src.handler.TecentGuoFuTaskHandler import TecentGuoFuTaskHandler


def setup_logging():
    # 创建一个日志记录器
    logger = logging.getLogger()
    logger.setLevel(logging.INFO)  # 设置全局日志级别

    # 创建一个文件处理器，使用日志轮转
    file_handler = RotatingFileHandler('app.log', maxBytes=1024 * 1024 * 5, backupCount=5)
    file_handler.setLevel(logging.DEBUG)  # 设置文件处理器的日志级别

    # 创建一个控制台处理器
    console_handler = logging.StreamHandler()
    console_handler.setLevel(logging.INFO)  # 设置控制台处理器的日志级别

    # 创建一个日志格式化器
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')

    # 将格式化器添加到处理器
    file_handler.setFormatter(formatter)
    console_handler.setFormatter(formatter)

    # 将处理器添加到日志记录器
    # logger.addHandler(file_handler)
    logger.addHandler(console_handler)


def basedir():
    if getattr(sys, 'frozen', None):
        return sys._MEIPASS
    else:
        return os.path.dirname(__file__)


def resource_path():
    return os.path.join(basedir(), "resources")


if __name__ == '__main__':
    setup_logging()
    parser = argparse.ArgumentParser(description="Top-level command")
    subparsers = parser.add_subparsers(dest='command', help='Sub-commands', required=True)
    config = HandlerConfig(basedir())
    close_handler = CloseComputerHandler(subparsers, config)
    dingtalk_handler = PushDingTalkHandler(subparsers, config)
    auto_office_handler = PushAutoOfficeHandler(subparsers, config)
    tecent_guo_fu_handler = TecentGuoFuTaskHandler(subparsers, config)
    browser_open_handler = BrowserOpenHandler(subparsers, config)
    process_handler = ProcessHandler(subparsers, config)
    soft_stone_handler = SoftStoneTaskHandler(subparsers, config)

    resource_path = resource_path()
    args = parser.parse_args()
    args.func(args)
